package com.headStorm.concurrent.lock;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author zhangqq
 * @create 2021-07-13 15:26
 **/
public class ReentrantLockDemo1 {

    private final Lock lock = new ReentrantLock();
    private int count = 0;

    public int add(int n) {
        lock.lock();
        try {
            count += n;
        } finally {
            lock.unlock();
            return count;
        }
    }

    public static void main(String[] args) {
        ReentrantLockDemo1 reentrantLockDemo1 = new ReentrantLockDemo1();
        ExecutorService cachedThreadPool = Executors.newFixedThreadPool(5);
        cachedThreadPool.execute(new Runnable() {
            @Override
            public void run() {
                while (true){
                    System.out.println(reentrantLockDemo1.add(1));
                }
            }
        });
    }

}
